<template>
  <div>
    <template
        v-if="onlyItem(item, item.children) && (!onlyOneChild.children || onlyOneChild.oneChild)">
        <el-menu-item class="menu-item" :index="resolvePath(onlyOneChild.path)">
          <template #title>
            <el-icon v-if="item.meta && item.meta.icon">
              <Component :is="item.meta.icon"/>
            </el-icon>
            <span>{{ onlyOneChild.meta.title }}</span>
          </template>
        </el-menu-item>
    </template>

    <el-sub-menu v-else :index="resolvePath(item.path)">
      <template #title>
        <el-icon>
          <Component :is="item.meta.icon"/>
        </el-icon>
        <span>{{item.meta.title}}</span>
      </template>

      <sidebar-item
          v-for="(child, index) in item.children"
          :key="child.path + index"
          :is-nest="true"
          :item="child"
          :base-path="resolvePath(child.path)"
          class="nest-menu"
      />
    </el-sub-menu>
  </div>

</template>
<script lang="ts" setup>
import {ElSubMenu, ElMenuItem, ElIcon} from "element-plus";
import {ref} from "vue"

const onlyOneChild = ref({});
const props = defineProps({
  item: {
    type: Object,
    required: true,
  },
  basePath: {
    type: String,
    default: "",
  },
});

function onlyItem(parent: any, children: any[]) {
  if (!children) children = []
  const hasChild = children.filter(item => {
    onlyOneChild.value = item
    return true
  })
  if (hasChild.length === 1) {
    return true
  }
  if (hasChild.length === 0) {
    onlyOneChild.value = {...parent, path: '', oneChild: true}
    return true
  }
  return false
}

const resolvePath = (path: string) => {

  if (path) {
    return props.basePath + "/" + path;
  }
  return props.basePath;
};
</script>
<style lang="scss" scoped>
.link-press {
  text-decoration: none;
}
</style>
